home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Mousetools / MachIV / SimpleBlanker.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  6KB  |  205 lines

  1. #ifdef AZTEC_C
  2. #include <functions.h>
  3. #else
  4. #include <clib/exec_protos.h>
  5. #include <clib/graphics_protos.h>
  6. #include <clib/intuition_protos.h>
  7. #endif
  8.  
  9. #include <stdlib.h>
  10. #include <exec/ports.h>
  11. #include <intuition/intuition.h>
  12.  
  13. /*
  14.  * Simple blanking skeleton. This demonstrates a simply way to implement your
  15.  * own screen blanker that receives messages from MachIV. This eliminates
  16.  * the need for you to create an input handler or timer routines.
  17.  *
  18.  * Feel free to create your own customized screen blanker. While this code is
  19.  * Copyright 1991 by Brian Moats, you may use it and distibute both this code
  20.  * and yours anywhere that you want. When distributing this code, please do
  21.  * not alter it in anyway.
  22.  *
  23.  * This program is a bare bones skeleton. It merely opens and closes a screen
  24.  * when told to by MachIV. The screen is red so that you know that it is not
  25.  * MachIV's own blanker. You need only add your screen opening where it says
  26.  * // INSERT YOUR SCREEN OPENING HERE!
  27.  * Add you graphics drawing routines where it says
  28.  * // INSERT YOUR GRAPHIC ROUTINES HERE!
  29.  *
  30.  * MachIV will execute a macro named "Blank" when the timeout says to blank
  31.  * the screen. It also executes a macro named "Unblank" when screen blanking
  32.  * should terminate and a macro named "MachIVQuit" when MachIV is quiting.
  33.  * These macros could anything, but to communicate with SimpleBlanker, they
  34.  * would be something like this:
  35.  *
  36.  * MSC_PUTMSG"MyScreenBlanker,1"     - Name this macro Blank.
  37.  * MSC_PUTMSG"MyScreenBlanker,2"     - Name this macro Unblank.
  38.  * MSC_PUTMSG"MyScreenBlanker,3"     - Name this macro MachIVQuit.
  39.  *
  40.  * SimpleBlanker waits on its port named "MyScreenBlanker" for one of three
  41.  * signals. When it receives a BLANK (1) signal, it loops doing whatever
  42.  * graphics on the screen that you program in. In this loop it also checks
  43.  * for an UNBLANK (2) or QUIT (3) signal. Messages sent are actually
  44.  * IntuiMessages structs but only Class, Code and Qualifier are filled in by
  45.  * MachIV. Only Class is used in this program.
  46.  *
  47.  * SimpleBlanker also quits when you execute it a second time.
  48.  *
  49.  * Compiles under Aztec 5.2a and SAS/C 5.10a.
  50.  *
  51.  * Aztec:
  52.  *
  53.  *     cc SimpleBlanker
  54.  *     ln SimpleBlanker.o -lc
  55.  *
  56.  * Lattice:
  57.  *
  58.  *     lc SimpleBlanker
  59.  *     blink from lib:c.o SimpleBlanker.o to SimpleBlanker LIB lib:lc.lib \
  60.  *           lib:amiga.lib SC SD
  61.  *
  62.  * Both compilers allow the // comment and I've used it here. If you are
  63.  * using an older compiler, you will need to change them to the standard
  64.  * comment form which I can't even show you here or it would be a nested
  65.  * comment!
  66.  */
  67.  
  68. // Signals sent from MachIV to your port.
  69.  
  70. #define BLANK   1
  71. #define UNBLANK 2
  72. #define QUIT    3
  73.  
  74. // Screen blanking constants.
  75.  
  76. #define SCREEN_DEPTH 1
  77. #define SCREEN_WIDTH 320
  78. #define SCREEN_HEIGHT 200
  79.  
  80. // Proto declaration.
  81.  
  82. static void die(void);
  83.  
  84.  
  85. // Local vars
  86.  
  87. struct IntuitionBase *IntuitionBase;
  88. struct GfxBase *GfxBase;
  89.  
  90. char *portname = "MyScreenBlanker";
  91.  
  92. struct NewScreen newScreen = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_DEPTH,
  93.                               -1,-1,0,CUSTOMSCREEN,NULL,NULL,NULL,NULL};
  94. struct Screen *s;
  95.  
  96. struct MsgPort      *portptr,*rp;
  97. struct IntuiMessage *imsg, quitmsg;
  98.  
  99. ULONG action;  // Will receive the IntuiMessage Class value.
  100.  
  101.  
  102. main(int argc, char **argv)
  103. {
  104.   // First see if we are already running. If so, Put a message telling it
  105.   // to quit. Wait for SimpleBlanker to Reply.
  106.  
  107.   if ((portptr = FindPort((STRPTR)portname)) != NULL) {
  108.     if (rp = CreateMsgPort()) {   // Create a reply port for us.
  109.       quitmsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
  110.       quitmsg.ExecMessage.mn_ReplyPort = rp;
  111.       quitmsg.ExecMessage.mn_Length = 32;
  112.       quitmsg.Class = QUIT;
  113.       PutMsg(portptr,(struct Message*)&quitmsg);
  114.       WaitPort(rp); // Wait for SimpleBlanker does a ReplyMsg();
  115.       GetMsg(rp);
  116.       DeleteMsgPort(rp);
  117.       exit(0);
  118.     }
  119.   }
  120.   // Port not found so create and add our port.
  121.  
  122.   if ((portptr = CreateMsgPort()) == NULL)
  123.     exit(0);
  124.  
  125.   portptr->mp_Node.ln_Pri = 1;         // Will be searched for.
  126.   portptr->mp_Node.ln_Name = portname;
  127.  
  128.   AddPort(portptr);
  129.  
  130.   IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE*)"intuition.library",37L);
  131.   GfxBase = (struct GfxBase *) OpenLibrary((UBYTE*)"graphics.library",37L);
  132.  
  133.   for (;;) {
  134.     WaitPort(portptr); // Waiting for BLANK or QUIT
  135.     while (imsg = (struct IntuiMessage *)GetMsg(portptr)) {
  136.       action = imsg->Class;
  137.       ReplyMsg((struct Message *)imsg);
  138.  
  139.       switch (action) {
  140.         case BLANK:
  141.  
  142. // INSERT YOUR SCREEN OPENING HERE!
  143.  
  144.           // MachIV says to blank so insert your own stuff here. You can do
  145.           // pretty much want you want, just be sure to check every so often
  146.           // for an UNBLANK or QUIT message.
  147.           // For demonstration purposes only, I just open a screen and set
  148.           // it to red.
  149.  
  150.           if ((s = OpenScreen(&newScreen)) == NULL)
  151.             break;
  152.           SetRGB4(&s->ViewPort,0L,15L,0L,0L); // Simply displays a red screen.
  153.           SetRGB4(&s->ViewPort,1L,15L,0L,0L);
  154.           SetRast(&s->RastPort,0L);
  155.  
  156.           // Check for any messages.
  157.  
  158.           while ((imsg = (struct IntuiMessage *)GetMsg(portptr)) == NULL) {
  159.  
  160. // INSERT YOUR GRAPHIC ROUTINES HERE!
  161.  
  162.             // You can put you drawing or animation routines calls here.
  163.             // A little Delay() here is nice to the system.
  164.  
  165.             Delay(5L);
  166.           }
  167.           // Got a message.
  168.  
  169.           action = imsg->Class;
  170.           ReplyMsg((struct Message *)imsg);
  171.           if (action == QUIT)
  172.             die();                    // Will never return.
  173.           else if (action != UNBLANK)
  174.             break;                   // UNBLANK falls through!!
  175.         case UNBLANK:
  176.           if (s)
  177.             CloseScreen(s);
  178.           s = NULL;
  179.           break;
  180.         case QUIT:
  181.           die();
  182.           break;
  183.       }
  184.     }
  185.   }
  186. }
  187.  
  188. // This deletes and closes up everything.
  189.  
  190. static void die(void)
  191. {
  192.   RemPort(portptr);  // portptr must be valid or we wouldn't be here.
  193.  
  194.   DeleteMsgPort(portptr);
  195.  
  196.   if (s)
  197.     CloseScreen(s);
  198.  
  199.   if (IntuitionBase)
  200.     CloseLibrary((struct Library*)IntuitionBase);
  201.   if (GfxBase)
  202.     CloseLibrary((struct Library*)GfxBase);
  203.   exit(0);
  204. }
  205.